package com.jiutong.meipenggang.ui.activitys;


import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiutong.meipenggang.R;
import com.jiutong.meipenggang.base.AppManager;
import com.jiutong.meipenggang.base.BaseActivity;
import com.jiutong.meipenggang.common.Constants;
import com.jiutong.meipenggang.entity.BaseBean;
import com.jiutong.meipenggang.http.model.interfaces.RegisterAndLoginBiz;
import com.jiutong.meipenggang.http.present.UserInfoPresent;
import com.jiutong.meipenggang.utils.SpConfig;
import com.jiutong.meipenggang.utils.StringUtil;
import com.jiutong.meipenggang.views.dialog.BaseDialog;
import com.orhanobut.logger.Logger;

import butterknife.Bind;
import butterknife.OnClick;

public class ReSetPwdActivity extends BaseActivity implements RegisterAndLoginBiz {


    @Bind(R.id.rl_common_title_back)
    RelativeLayout rlCommonTitleBack;
    @Bind(R.id.tv_common_title)
    TextView tvCommonTitle;
    @Bind(R.id.et_reset_pwd)
    EditText etResetPwd;
    @Bind(R.id.iv_reset_pwd)
    ImageView ivResetPwd;
    @Bind(R.id.et_reset_pwd_re)
    EditText etResetPwdRe;
    @Bind(R.id.iv_reset_pwd_re)
    ImageView ivResetPwdRe;
    @Bind(R.id.btn_reset_pwd_submit)
    Button btnResetPwdSubmit;

    private boolean pwdisShow = true;
    private boolean newPwdisShow = true;

    private UserInfoPresent userInfoPresent;

    private BaseDialog dialog;
    private String phone;

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_re_set_pwd;
    }

    @Override
    protected void initView() {
        super.initView();
        tvCommonTitle.setText("重新设置登录密码");
        Bundle bundle = getIntent().getExtras();
        if (bundle == null) {
            return;
        } else {
            phone = bundle.getString("phone");
        }
        userInfoPresent = new UserInfoPresent(this, this);
    }

    @OnClick({R.id.rl_common_title_back, R.id.iv_reset_pwd, R.id.iv_reset_pwd_re, R.id.btn_reset_pwd_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_common_title_back:
                finish();
                break;
            case R.id.iv_reset_pwd:
                if (pwdisShow) {
                    ivResetPwd.setBackgroundResource(R.mipmap.eye_close);
                    etResetPwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    pwdisShow = false;
                } else {
                    ivResetPwd.setBackgroundResource(R.mipmap.eye_open);
                    etResetPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    pwdisShow = true;
                }
                break;
            case R.id.iv_reset_pwd_re:
                if (newPwdisShow) {
                    ivResetPwdRe.setBackgroundResource(R.mipmap.eye_close);
                    etResetPwdRe.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    newPwdisShow = false;
                } else {
                    ivResetPwdRe.setBackgroundResource(R.mipmap.eye_open);
                    etResetPwdRe.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    newPwdisShow = true;
                }
                break;
            case R.id.btn_reset_pwd_submit:
                if (isRight()) {
                    userInfoPresent.forgetPwd();
                }
                break;
        }
    }

    public void showDialog() {
        if (dialog == null) {
            dialog = new BaseDialog.Builder().
                    layResuorce(R.layout.dialog_common).
                    cancelViewClickEvent(R.id.tv_dialog_cancel).
                    okViewClickEvent(R.id.tv_dialog_ok).
                    ok2ViewClickEvent(R.id.tv_dialog_confirm).
                    confirmId(R.id.tv_dialog_confirm).
                    llBottomId(R.id.ll_dialog).
                    contentTextId(R.id.tv_dialog_content).
                    hintTextId(R.id.tv_dialog_hint).
                    gravityStyle(Gravity.CENTER).
                    setHintText("温馨提示").
                    setContentText("修改密码成功，请重新登录").
                    setConfirmText("确定").
                    showOneOrTwoBtn(false).
                    animResource(R.style.dialog_animation_fade).
                    outSideCancel(false).
                    dialogWidth(-2).
                    build();
            dialog.setOnDialogClickListener(new BaseDialog.OnDialogClickListener() {
                @Override
                public void onOkClick() {

                }

                @Override
                public void onOk2Click() {
                    dialog.dismiss();
                    Logger.d("telephone",SpConfig.getInstance().getString(Constants.USERNAME_STRING));
                    SpConfig.getInstance().edit().putString(Constants.SHOU_SHI, "").commit();
                    if (TextUtils.isEmpty(SpConfig.getInstance().getString(Constants.USERNAME_STRING))) {
                        openActivity(LoginActivity.class);
                    } else  {
                        openActivity(LoginedActivity.class);
                    }
                    finish();
                }
            });
            dialog.show(getSupportFragmentManager(), TAG);
        }
    }

    private boolean isRight() {
        if (TextUtils.isEmpty(etResetPwd.getText().toString().trim())) {
            toastShortShow("请输入新密码");
            return false;
        }

        if (!StringUtil.isRightPwd(etResetPwd.getText().toString().trim())) {
            toastShortShow("密码格式错误");
            return false;
        }
        if (TextUtils.isEmpty(getNewPwd())) {
            toastShortShow("请再次输入新的密码");
            return false;
        }
        if (!getNewPwd().equals(etResetPwdRe.getText().toString().trim())) {
            toastShortShow("密码确认错误");
            return false;
        }
        return true;
    }

    @Override
    public String getPhoneNum() {
        return phone;
    }

    @Override
    public String getVerificatCode() {
        return null;
    }

    @Override
    public String getOriPwd() {
        return etResetPwd.getText().toString().trim();
    }

    @Override
    public String getNewPwd() {
        return etResetPwdRe.getText().toString().trim();
    }

    @Override
    public void onVerCodeGetSuccess(BaseBean subjects) {

    }


    @Override
    public void onSuccess(Object object) {
        BaseBean baseBean = (BaseBean) object;
        if (baseBean.getError() == -1) {
            showDialog();
        } else {
            toastShortShow(baseBean.getMsg());
        }
    }

    @Override
    public void onError(Throwable e) {


    }

    @Override
    public int getOpt() {
        return 7;
    }
}
